package com.ytc.user.controller;

import com.ytc.user.model.User;
import com.ytc.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/regist")
    @ResponseBody
    public void registUser(User user){
        System.out.println(user.toString());
        userService.addUser(user);
    }

    @RequestMapping("/removeSession")
    public String removeSession(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "login";
    }

    @RequestMapping("/toChangePass")
    public String toChangePass(Model model,HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("user");
        System.out.println("修改密码："+user.getUserRole());
        model.addAttribute("user2",user);
        return "account/changePass";
    }
    @RequestMapping("/changeUserPass")
    @ResponseBody
    public void changeUserPass(User user,HttpServletRequest request){
        System.out.println("修改密码："+user.toString());
        userService.changeUserPass(user);
        User user1 =(User) request.getSession().getAttribute("user");
        user1.setUserPass(user.getUserPass());
        request.getSession().removeAttribute("user");
        request.getSession().invalidate();
        request.getSession().setAttribute("user",user1);
    }

}
